<?php session_start();
include "constants.php";
$inmail=INEMAIL;
$NEWPROCESS=NEWPROCESS;

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); 

if (!$link) { 
    die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db(DB_NAME);


if ((isset($_POST['appstatus'])) && (strlen(trim($_POST['appstatus'])) > 0)) {
	$FinalStatus = stripslashes(strip_tags($_POST['appstatus']));
} else {$FinalStatus = '0';}
if ((isset($_POST['appdecision'])) && (strlen(trim($_POST['appdecision'])) > 0)) {
	$FinalDecision = stripslashes(strip_tags($_POST['appdecision']));
} else {$FinalDecision = '0';}
if ((isset($_POST['ProID'])) && (strlen(trim($_POST['ProID'])) > 0)) {
	$ProID = stripslashes(strip_tags($_POST['ProID']));
} else {$ProID = '0';}
if ((isset($_POST['A_Amount'])) && (strlen(trim($_POST['A_Amount'])) > 0)) {
	$A_Amount = stripslashes(strip_tags($_POST['A_Amount']));
} else {$A_Amount = '0';}
if ((isset($_POST['A_cost'])) && (strlen(trim($_POST['A_cost'])) > 0)) {
	$A_cost = stripslashes(strip_tags($_POST['A_cost']));
} else {$A_cost = '0';}
if ((isset($_POST['scheduledpayday'])) && (strlen(trim($_POST['scheduledpayday'])) > 0)) {
	$scheduledpayday = stripslashes(strip_tags($_POST['scheduledpayday']));
} else {$scheduledpayday = '';}
if ((isset($_POST['setuppadday'])) && (strlen(trim($_POST['setuppadday'])) > 0)) {
	$setuppadday = stripslashes(strip_tags($_POST['setuppadday']));
} else {$setuppadday = '';}
if ((isset($_POST['A_employee'])) && (strlen(trim($_POST['A_employee'])) > 0)) {
	$A_employee = stripslashes(strip_tags($_POST['A_employee']));
} else {$A_employee = '';}
if ((isset($_POST['AccID'])) && (strlen(trim($_POST['AccID'])) > 0)) {
	$AccID = stripslashes(strip_tags($_POST['AccID']));
} else {$AccID = '0';}
if ((isset($_POST['approvedcredits'])) && (strlen(trim($_POST['approvedcredits'])) > 0)) {
	$approvedcredits = stripslashes(strip_tags($_POST['approvedcredits']));
} else {$approvedcredits = '0';}
if($approvedcredits=='0'||$approvedcredits=='1'){$approvedcredits=0;}
if($approvedcredits==1){$approvedcredits=0;}
if ((isset($_POST['A_reason'])) && (strlen(trim($_POST['A_reason'])) > 0)) {
	$A_reason = stripslashes(strip_tags($_POST['A_reason']));
} else {$A_reason = '0';}
if ((isset($_POST['questionitem'])) && (strlen(trim($_POST['questionitem'])) > 0)) {
	$questionitem = stripslashes(strip_tags($_POST['questionitem']));
} else {$questionitem = '0';}

if ((isset($_POST['questionnum1'])) && (strlen(trim($_POST['questionnum1'])) > 0)) {
	$questionnum1 = stripslashes(strip_tags($_POST['questionnum1']));
} else {$questionnum1 = '';}

if ((isset($_POST['questionnum2'])) && (strlen(trim($_POST['questionnum2'])) > 0)) {
	$questionnum2 = stripslashes(strip_tags($_POST['questionnum2']));
} else {$questionnum2 = '';}

if ((isset($_POST['A_payment'])) && (strlen(trim($_POST['A_payment'])) > 0)) {
	$Finalpayment = stripslashes(strip_tags($_POST['A_payment']));
} else {$Finalpayment = '0';}

if ((isset($_POST['newpayamount'])) && (strlen(trim($_POST['newpayamount'])) > 0)) {
	$newpayamount = stripslashes(strip_tags($_POST['newpayamount']));
} else {$newpayamount = '0';}

if ((isset($_POST['partialpay'])) && (strlen(trim($_POST['partialpay'])) > 0)) {
	$partialpay = stripslashes(strip_tags($_POST['partialpay']));
} else {$partialpay = '0';}

if ((isset($_POST['newpadday'])) && (strlen(trim($_POST['newpadday'])) > 0)) {
	$newpadday = stripslashes(strip_tags($_POST['newpadday']));
} else {$newpadday = '';}

if ((isset($_POST['defaultreason'])) && (strlen(trim($_POST['defaultreason'])) > 0)) {
	$defaultreason = stripslashes(strip_tags($_POST['defaultreason']));
} else {$defaultreason = '';}

if ((isset($_POST['expectedpay'])) && (strlen(trim($_POST['expectedpay'])) > 0)) {
	$expectedpay = stripslashes(strip_tags($_POST['expectedpay']));
} else {$expectedpay = '';}

if ((isset($_POST['alreadypay'])) && (strlen(trim($_POST['alreadypay'])) > 0)) {
	$alreadypay = stripslashes(strip_tags($_POST['alreadypay']));
} else {$alreadypay = '';}

if ((isset($_POST['defaultreason'])) && (strlen(trim($_POST['defaultreason'])) > 0)) {
	$defaultreason = stripslashes(strip_tags($_POST['defaultreason']));
} else {$defaultreason = '';}


$PAIDAMOUNT=$expectedpay+$alreadypay;
$NSFPAIDAMOUNT=$partialpay+$alreadypay;


//$FinalDecision=$_POST["appdecision"];
//$FinalStatus=$_POST["appstatus"];
//$Finalpayment=$_POST["A_payment"];
//$FinalNSFpayment=$_POST["NSFpayments"];

$Applicant="SELECT * FROM Profiles WHERE ProfileID = '$ProID'";
$AppResu = mysql_query($Applicant,$link);
$rowApp = mysql_fetch_array($AppResu);
$FirstName=$rowApp['FirstName'];
$Email=$rowApp['Email'];
$CNum=$rowApp['CreditNum'];

if($FinalStatus!=6){
if(!isset($Email))
{echo "You are in the wrong page, please start your ". "<a href='apply.php'>application here</a> now, thank you for your cooperation!";
exit();
}}

if($FinalStatus == 1 && $FinalDecision==3) {
$Approval = "UPDATE Applications SET FirstPayDay='$scheduledpayday',ApprovalAmount='$A_Amount', TotalCost='$A_cost', ApprovalDate=NOW(),NewApp='7',CollectDate='$setuppadday',Employee='$A_employee',Credits='$approvedcredits' WHERE AccountID = '$AccID'";
$ApprovalResu = mysql_query($Approval,$link);

//to send out email
$subject = "Application Approved";
$message = "Dear ".$FirstName.
",\n\n Congratulations! Your application for a payday loan via www.cash2u.ca has been approved according to our policy. You are qulified for payday loan amount CAD$ ". $_POST['A_Amount']." which will be deposited into your banking account in 24 hours. Please check your account and if you don't receive this payday loan within 24 hours (please exclude Saturday, Sunday and National holidays since there is no interbank transaction during these days. Pleas wait until the next business day,e.g., Monday!), please let us know as soon as possible.\n\nThank you for choosing www.cash2u.ca!\n\nCash2u.ca appreciates your business!\n\n";
$message=wordwrap($message,70);
$Froms="From:".$inmail;
mail($Email, $subject,$message,$Froms);

}

if($FinalStatus == 1 && $FinalDecision==5) {
$Refuse="UPDATE Profiles SET Credit='REFUSE',CreditNum=$CNum+1,LastDateCredit=NOW(),Reasons='$A_reason' WHERE ProfileID = '$ProID'";
$RefuseUpdate=mysql_query($Refuse,$link);

$AppUpdate = "UPDATE Applications SET ApprovalDate=NOW(),NewApp='5',Employee='$A_employee' WHERE AccountID = '$AccID'";
$ApprovalResu = mysql_query($AppUpdate ,$link);

//send email
$subject = "Application Refused";
$message = "Dear ".$FirstName.
",\n\n We are very sorry to let you know that you are not qualified for our payday loan policy due to the information you provided to us. We welcome you come back later once you can provide us with new information which is qualified for the payday loan policy. 
\n\nPlease don't response to this emal since no one is able to answer you.\n\nThank you for choosing www.cash2u.ca!\n\nCash2u.ca appreciates your business!\n\n";
$message=wordwrap($message,70);
$Froms="From:".$inmail;
mail($Email, $subject,$message,$Froms);

}

if($FinalStatus == 1 && $FinalDecision==10) {
	if($questionitem==1){
		$subject = "Your security questions";
		$message = "Dear ".$FirstName.
		",\n\nWe are not going to lend out any loan if we can't verify the banking information.
		\n\nCould you please provide your onince security questions/answers for the approval process via this email?
		\n\nThanks for your cooperation and business!
		\n\nCash2u.ca Team";
		$message=wordwrap($message,70);
		$Froms="From:".$inmail;
		mail($Email, $subject,$message,$Froms);

	} else if($questionitem==2){
		$subject = "Invalid debit # or password";
		$message = "Dear ".$FirstName.
		",\n\nWe are not going to lend out any loan if we can't verify the banking information.
		\n\nEither your debit card # or your password is invaid.
		\n\nCould you please double check these information and provide the correct debit# or password for the approval process via this email?
		\n\nThanks for your cooperation and business!\n\nCash2u.ca Team";
		$message=wordwrap($message,70);
		$Froms="From:".$inmail;
		mail($Email, $subject,$message,$Froms);
	} else if($questionitem==3){
		$AppUpdate = "UPDATE Applications SET SQ4='$questionnum1',SQ5='$questionnum2' WHERE AccountID = '$AccID'";
		$ApprovalResu = mysql_query($AppUpdate ,$link);

		$subject = "More security questions";
		$message = "Dear ".$FirstName.
		",\n\nWe are not going to lend out any loan if we can't verify the banking information.		
		\n\nCould you please provide the following security questions for the approval process via this email?
		\n\n".$questionnum1."\n\n".$questionnum2."
		\n\nThanks for your cooperation and business!\n\nCash2u.ca Team";
		$message=wordwrap($message,70);
		$Froms="From:".$inmail;
		mail($Email, $subject,$message,$Froms);
		
	} else if($questionitem==4){
		$AppUpdate = "DELETE FROM Applications WHERE AccountID = '$AccID'";
		$ApprovalResu = mysql_query($AppUpdate ,$link);
	} else if($questionitem==5){
		$subject = "Received your reloan application";
		$message = "Dear ".$FirstName.
		",\n\nWe are waiting for your payment to be completed, once our system shows your payment is clear, we will approve your reloan application.
		\n\nThanks for your cooperation and business!\n\nCash2u.ca Team";
		$message=wordwrap($message,70);
		$Froms="From:".$inmail;
		mail($Email, $subject,$message,$Froms);		
	}

}

if($FinalStatus == 3||$FinalStatus == 4||$FinalStatus == 9) {

	if($Finalpayment==2) {
		$subject = "Clear Loan Repayment";
		$message = "Dear ".$FirstName.
		",\n\nYour payday loan repayment is clear now. If this is your first time to apply for the payday loan via www.cash2u.ca, congratulation! You become our member now. Please remember to use your login ID (your email) and password to apply for your payday loan as a existing customer whe you come back. 
		\n\nThank you for choosing www.cash2u.ca!
		\n\nCash2u.ca appreciates your business!
		\n\nCash2u Team";
		$message=wordwrap($message,70);
		$Froms="From:".$inmail;
		mail($Email, $subject,$message,$Froms);
		
		$paidloan = "UPDATE Applications SET NewApp='2',PaidAmount='$PAIDAMOUNT',Employee='$A_employee' WHERE AccountID = '$AccID'";
		$paidloanResu = mysql_query($paidloan,$link);

	} else if($Finalpayment==22) {
		$paidloan = "UPDATE Applications SET NewApp='2',PaidAmount='$PAIDAMOUNT',Employee='$A_employee' WHERE AccountID = '$AccID'";
		$paidloanResu = mysql_query($paidloan,$link);
	} else if($Finalpayment==4) {
		$subject = "Loan Repayment Notice";
		$message = "Dear ".$FirstName.
		",\n\nDue to NSF in your account, your repayment(".($expectedpay-$partialpay).") is returned to your bank account. We will collect your repayment at ".$newpayamount."(including NSF charges) again on your next payday. 
		Please keep enough fund in your bank account to avoid unnecessary NSF charges. 
		\n\nIf you don't repay us, you are not permitted to reloan from us. We will send your information to our third party collection agency who will contact you directly to collect yoru repayment plus cost and they will report your default case to credti bureau which will have negative impact on your credit score.
		\n\nThank you for choosing www.cash2u.ca!\n\nCash2u.ca appreciates your business!\n\nCash2u Team";

		$paidNSF="UPDATE Profiles SET Credit='NSF',CreditNum=$CNum+1,LastDateCredit=NOW(),Reasons='NSF' 
		WHERE ProfileID = '$ProID'";
		$paidNSFresu=mysql_query($paidNSF,$link);

		$AppUpdate = "UPDATE Applications SET NewApp='4',CollectDate='$newpadday',PaidAmount='$NSFPAIDAMOUNT',NewAmount='$newpayamount', Employee='$A_employee' 
		WHERE AccountID = '$AccID'";
		$AppResu = mysql_query($AppUpdate ,$link);

	} else if($Finalpayment==44) {
		$subject = "Loan Repayment Notice";
		$message = "Dear ".$FirstName.
		",\n\nDue to NSF in your account, your repayment(".($expectedpay-$partialpay).") is returned to your bank account. As a valued customer, We will leave this payment as your new loan and collect your repayment at ".$newpayamount." on your next payday. 
		Please keep enough fund in your bank account to avoid unnecessary NSF charges. 
		\n\nIf you don't repay us, you are not permitted to reloan from us. We will send your information to our third party collection agency who will contact you directly to collect yoru repayment plus cost and they will report your default case to credti bureau which will have negative impact on your credit score.
		\n\nThank you for choosing www.cash2u.ca!\n\nCash2u.ca appreciates your business!\n\nCash2u Team";

		$paidNSF="UPDATE Profiles SET Credit='NSF',CreditNum=$CNum+1,LastDateCredit=NOW(),Reasons='NSF' 
		WHERE ProfileID = '$ProID'";
		$paidNSFresu=mysql_query($paidNSF,$link);

		$AppUpdate = "UPDATE Applications SET NewApp='4',CollectDate='$newpadday',PaidAmount='$NSFPAIDAMOUNT',NewAmount='$newpayamount', Employee='$A_employee' 
		WHERE AccountID = '$AccID'";
		$AppResu = mysql_query($AppUpdate ,$link);

	} else if($Finalpayment==8) {
		//Application
		$AppUpdate = "UPDATE Applications SET NewApp='8',Employee='$A_employee' WHERE AccountID = '$AccID'";
		$AppResu = mysql_query($AppUpdate ,$link);	

		$paidNSF="UPDATE Profiles SET Credit='Default',CreditNum=$CNum+1,LastDateCredit=NOW(),Reasons='$defaultreason' WHERE ProfileID = '$ProID'";
		$paidNSFresu=mysql_query($paidNSF,$link);

	} else if($Finalpayment==9) {
		//send email
		$subject = "Collection Agency will collect Your payday loan repayment";
    		$message = "Dear ".$FirstName.
		",\n\nDue to your non-payment loan from us, we have sent your case to our collection agency partner to collect your unpaid payday loan. 
		\nYour total payment will be $".$newpayamount.". You can't borrow from us again and our collection agency/lawyer will contact you directly to collect your repayment. 
		\nCash2u.ca appreciates your business!
		\nCash2.ca Team\n";

		$message=wordwrap($message,70);
		$Froms="From:".$inmail;
		mail($Email, $subject,$message,$Froms);

		$AppUpdate = "UPDATE Applications SET NewApp='9',PaidAmount='$NSFPAIDAMOUNT',NewAmount='$newpayamount', Employee='$A_employee' 
		WHERE AccountID = '$AccID'";
		$AppResu = mysql_query($AppUpdate ,$link);		
	}

}


if($FinalStatus ==7) {
$Approval = "UPDATE Applications SET NewApp='3' WHERE AccountID = '$AccID'";
$ApprovalResu = mysql_query($Approval,$link);
}


?>
<head>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Cash2u.ca: Payday loan Application process</title>
<META NAME="Description" CONTENT=Cash2u.ca offers online payday loans and cash advance for Canadians">
<script type="text/javascript">
	window.open('', '_self', ''); window.setTimeout("window.close()", 3000);
</script>
</head>
<body>
<div id="maincontainer">

        <form method="post" action="">
	<table>	
	<tr>
<?php if($FinalStatus == 1 && $FinalDecision==3) { ?>
	<td width="740" align="left">New application has been approved,</br></br>

A payday loan from applicant <?php echo $FirstName;?> has been approved. The scheduled repay day is <?php echo $_POST['A_payday'];?></br></br>

		</td> <?php } ?>

<?php if($FinalStatus == 1 && $FinalDecision==5) { ?>
	<td width="740" align="left">New application has been refused,</br></br>

A payday loan from applicant <?php echo $FirstName;?> has been refused due to <?php echo $_POST['A_reason'];?></br></br>

		</td> <?php } ?>

<?php if($FinalStatus == 1 && $FinalDecision==10) { ?>
	<td width="740" align="left">questions,</br></br>

More questions need to ask applicant <?php echo $FirstName;?> </br></br>

		</td> <?php } ?>

<?php if($FinalStatus == 3) { ?>
	<td width="740" align="left">Loan Repayment Process</br></br>

This is loan payment process</br></br>

		</td> <?php } ?>

<?php if($FinalStatus == 4) { ?>
	<td width="740" align="left">NSF Process</br></br>

This is NSF process</br></br>

		</td> <?php } ?>

<?php if($FinalStatus == 9) { ?>
	<td width="740" align="left">Agency Process</br></br>

This is to send Agency process</br></br>

		</td> <?php } ?>


<?php if($FinalStatus == 7) { ?>
	<td width="740" align="left">ACH Process,</br></br>

Payment and deposit has been processed for this customer</br></br>

		</td> <?php } ?>

		</tr>

	</table>

	</form>
	
	
</div>
</body>
</html>
